package cate.game.activity.weekcard.po;

import cate.common.table.activity.weekcard.row.WeekCardRow;
import cate.common.table.d.GDOperation;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

public class WeekCardItemPo extends RoleDependent {
    public int id;
    @NoteField("周卡是否已购买")
    public boolean buy;
    @NoteField("周卡每日奖励领取记录list")
    public List<Integer> drawList;

    public WeekCardItemPo() {
    }

    public WeekCardItemPo(int id) {
        this.id = id;
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (drawList == null) {
            drawList = new ArrayList<>();
        }
    }

    public WeekCardItemPo(int id, boolean buy) {
        this.id = id;
        this.buy = buy;

    }


    @JsonIgnore
    public MixRes getReward(int day) {
        return calDailyReward(day+1);
    }

    @JsonIgnore
    public int getMailId() {
        return role.getGame().table.weekCard.weekCard.get(id).mailId;
    }

    @JsonIgnore
    public WeekCardRow getRow() {
        return role.getGame().table.weekCard.weekCard.get(this.id);
    }

    //计算邮件要发送的奖励，不包含当天的奖励
    public MixRes calDailyReward(int day) {
        MixRes res = new MixRes();
        if (this.buy) {
            for (int i = 1; i < day; i++) {
                if (!drawList.contains(i)) {
                    res.addList(new MixRes(getRow().dailyReward));
                    drawList.add(i);
                }
            }
        }
        return res;
    }

    //发送奖励邮件
    private void sendMail(MixRes reward, int mailID) {
        MailBuilder mailBuilder = new MailBuilder();
        mailBuilder.setContentId(mailID).setReward(reward.copy())
                .setOperation(GDOperation.WEEK_CARD);
        role.getMail().add(mailBuilder.build());
    }

    //补发奖励
    public void reissue(int day) {
        MixRes res = calDailyReward(day);
        if (res.isEmpty()) {
            return;
        }
        sendMail(res, getRow().mailId);
    }
}
